package com.jiang.controller;

import com.alibaba.fastjson.JSON;
import com.jiang.bean.Classno;
import com.jiang.bean.QueryInfo;
import com.jiang.service.ClassnoService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
public class ClassnoController {

    @Resource
    ClassnoService classnoService;

    @RequestMapping(value = "/allClassno",produces = {"application/json;charset=UTF-8"})
    public String getAllClassno(QueryInfo queryInfo){
        int numbers = classnoService.getCountclass(queryInfo.getClassnum(),queryInfo.getCollege(),queryInfo.getHeadmaster());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Classno> list = classnoService.getAllClassno(queryInfo.getClassnum(),queryInfo.getCollege(),queryInfo.getHeadmaster(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",list);
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping(value = "/addClassno",produces = {"application/json;charset=UTF-8"})
    public String addClassno(@RequestBody Classno classno){
        int i = classnoService.addClassno(classno);
        String str = i >0 ? "success" : "error";
        return str;
    }

    @RequestMapping(value = "/delClassno",produces = {"application/json;charset=UTF-8"})
    public String delClassno(int id){
        int i = classnoService.deleteClassno(id);
        String str = i >0 ? "success" : "error";
        return str;
    }

    @RequestMapping(value = "/updateClassno",produces = {"application/json;charset=UTF-8"})
    public String updateClassno(@RequestBody Classno classno){
        int i = classnoService.updataClassno(classno);
        String str = i >0 ? "success" : "error";
        return str;
    }

    @RequestMapping(value = "/selectById",produces = {"application/json;charset=UTF-8"})
    public String selctById(int id){
        Classno classno = classnoService.selectById(id);
        String users_json = JSON.toJSONString(classno);
        return users_json;
    }
}
